package com.sdp.irrigation.db.irrigation_eq_real_time.service.impl;

import java.time.LocalDateTime;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.irrigation.db.irrigation_eq_real_time.mapper.IrrigationEqRealTimeMapper;
import com.sdp.irrigation.db.irrigation_eq_real_time.service.IIrrigationEqRealTimeService;
import com.sdp.irrigation.entity.IrrigationEqRealTime;
import com.sdp.irrigation.entity.IrrigationPumpWell;
import com.sdp.irrigation.utils.IrrigationConstants;

/**
 * <p>
 * 设备实时信息 服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-12-21
 */
@Service
public class IrrigationEqRealTimeServiceImpl extends ServiceImpl<IrrigationEqRealTimeMapper, IrrigationEqRealTime> implements IIrrigationEqRealTimeService {

	@Override
	public IrrigationEqRealTime createDefaultEqRealTime(IrrigationPumpWell pumpWell) {
		IrrigationEqRealTime eqRealTime = new IrrigationEqRealTime();
		eqRealTime.setEqNo(pumpWell.getEqNo());
//		eqRealTime.setPwNo(pumpWell.getPwNo());
//		eqRealTime.setOnlineStatus(IrrigationConstants.设备在线状态_不在线.getCode());
//		eqRealTime.setIrrigationStatus(IrrigationConstants.设备灌溉状态_未灌溉.getCode());
//		eqRealTime.setFertilizationStatus(IrrigationConstants.设备施肥状态_未施肥中.getCode());
//		eqRealTime.setWateringStatus(IrrigationConstants.设备加水成功状态_未成功.getCode());
//		eqRealTime.setStirringStatus(IrrigationConstants.设备搅拌成功状态_未成功.getCode());
//		eqRealTime.setRepairStatus(IrrigationConstants.设备报修状态状态_未报修.getCode());
		eqRealTime.setCreateTime(LocalDateTime.now());
		eqRealTime.setUpdateTime(LocalDateTime.now());
		return eqRealTime;
	}

	@Override
	public IrrigationEqRealTime updateEqRealTime(String oldEqNo, IrrigationPumpWell pumpWell) {
		//构造默认的设备实时状态信息
		IrrigationEqRealTime eqRealTime = createDefaultEqRealTime(pumpWell);
		
		//根据机井编号查询对应的设备实时信息是否存在，如果存在则删除
		IrrigationEqRealTime dbEqRealTime = queryRealTimeByPwNo(pumpWell.getPwNo());
		if(!StringUtils.isEmpty(dbEqRealTime)) {
			removeRealTimeByPwNo(pumpWell.getPwNo());
		}
		//添加新的设备实时信息
		save(eqRealTime);
		
		return eqRealTime;
		
		
	}


	@Override
	public IrrigationEqRealTime queryRealTimeByPwNo(String pwNo) {
		LambdaQueryWrapper<IrrigationEqRealTime> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(IrrigationEqRealTime::getPwNo, pwNo);
		return getOne(lambdaQueryWrapper,false);
	}

	@Override
	public void removeRealTimeByPwNo(String pwNo) {
		LambdaQueryWrapper<IrrigationEqRealTime> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(IrrigationEqRealTime::getPwNo, pwNo);
		remove(lambdaQueryWrapper);
		
	}

}
